package top.leyi.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import top.leyi.entity.Role;
import top.leyi.entity.user.User;

import java.util.List;

public interface UserMapper {

    @Select("select * from user   ")
    public List<User> queryAllUserInfo( );

    @Select(
            "select u.*, c.* ,ur.role_id as roleId\n" +
                    "  from user u left join  \n" +
                    "(\n" +
                    "SELECT c.id, c.name as classname, col.name as department from class c\n" +
                    "left join college col on col.id = c.col_id)\n" +
                    " as c\n" +
                    "\n" +
                    " on c.id = u.classId\n" +
                    " join user_role ur \n" +
                    " on u.id = ur.user_id\n" +
                    " WHERE token =#{vaule}")
    public User queryUserInfo(String token);

            @Select("select u.*, c.* ,ur.role_id as roleId\n" +
                    "  from user u left join  \n" +
                    "(\n" +
                    "SELECT c.id, c.name as classname, col.name as department from class c\n" +
                    "left join college col on col.id = c.col_id)\n" +
                    " as c\n" +
                    "\n" +
                    " on c.id = u.classId\n" +
                    " join user_role ur \n" +
                    " on u.id = ur.user_id\n" +
                    " WHERE u.id  = #{value}")
    User queryOneUser(int id);

    @Select("select count(*) from user where  token = #{token}")
    int queryOneWithToken(String token);

    @Select("select * from user where id = #{id} and token = #{token}")
    User queryOneUserWithToken(User user);

    @Select("select * from Role r where r.id = (" +
            "select role_id from user_role where user_id = #{id})")
    Role queryUser(User user);

    @Select("select * from user WHERE openId = #{value}")
    User   queryUserWithOpenId(String openId);

    @Select("select count(*) from attention  where bigV = #{bigV} and fans  = #{fansId} and is_delete = 0")
    int queryIfAtten(int bigV, int fansId);

    /**
     * 查询个人的权限
     */
    @Select("SELECT  DISTINCT p.`name` FROM " +
            " privilege p join role_privilege rp on p.id = rp.privilege_id " +
            ", role r JOIN user_role ur on r.id = ur.role_id " +
            "where ur.user_id = #{id} and rp.role_id = r.id ")
    List<String> queryOwnPrivilege(User user);

    /**
     * 查询个人的权限
     */
    @Select("SELECT p.name from privilege p " +
            " join role_privilege  rp " +
            "on rp.privilege_id = p.id  " +
            "where rp.role_id =#{value} ")
    List<String> queryOwnPrivilege1(int roleId);



    @Select("select role_id from user_role where user_id = #{value}")
    int queryRoleId(int userId);


    /**
     * @desc 插入一个user对应的角色
     */
    @Insert("Insert into user_role values (#{id}, #{roleId})")
    public void addOneUserRole(User user);

    @Insert("INSERT INTO  user(token, openId) VALUES(#{token}, #{openId})")
    public void insertOneUser(User user) ;

    /**
     * @desc 获取最近增加的主键
     */
    @Select("SELECT LAST_INSERT_ID() AS id")
    public  int queryLastId();


    @Update("UPDATE user SET username = #{username}, wechat = #{wechat}, qq = #{qq}," +
            " phone = #{phone}, classId = #{classId}," +
            " email = #{email}, headPic = #{headPic}" +
            ", bgPic = #{bgPic} , word = #{word}," +
            "college = #{college}, study_time = #{study_time} WHERE id = #{id}")

    public void insertUserDetils(User user) ;


}
